<?php
session_start();
date_default_timezone_set('PRC'); 
include "../public/common/config.php";

$other = $_SESSION['currentOther'];

$user_id = $_SESSION['home_userid'];

$currentOtherNum = $_SESSION['chat'][$other];

$sqlOther = "select id num from chat where (send_id = {$user_id} and income_id = {$other}) or (income_id = {$user_id} and send_id = {$other})";
$resOther = mysql_query($sqlOther);
$nowNum = mysql_num_rows($resOther);

if($currentOtherNum < $nowNum){
	$str = '';
	$start = $currentOtherNum-1;
	$num = $nowNum-$currentOtherNum;
	$sqlChat = "
	(select chat.send_id,chat.income_id,chat.content,chat.create_time,user.username,user.img from chat left join user on(chat.send_id = user.id) where chat.send_id = {$other} and chat.income_id = {$user_id})
	union all
	(select chat.send_id,chat.income_id,chat.content,chat.create_time,user.username,user.img from chat left join user on(chat.send_id = user.id) where chat.send_id = {$user_id} and chat.income_id = {$other})
	order by create_time limit $start,$num
	";
//	echo $sqlChat;
	$resChat = mysql_query($sqlChat);
	
	while($rowChat = mysql_fetch_assoc($resChat)){
		$class = 'cont_item_left';
		if($rowChat['send_id'] == $user_id){
			$class = 'cont_item_right';
		}
		$str .= "<p><span>" . date("Y-m-d H-i-s", $rowChat['create_time']) . "</span></p><div class='" . 
			$class . "'><img src='img/" . $rowChat['img'] . "'/><p>" . 
			$rowChat['content'] . "</p></div>";
	}
	$info = array('status'=>1,'str'=>$str);
	$_SESSION['chat'][$other] = $nowNum;
}else{
	$info = array('status'=>0);
}
echo json_encode($info);


?>